//void, Obj this, Obj Victim
Item item;
int ap;

if (!.IsValid)
	return;

// Kills statistic
if(.IsValid){
	if(.IsAlive){
		if(!.IsDead){
		
			.AsUnit.IncKills(1); 

			if(.AsUnit.inherentlevel >= 10){
				if(Victim){
					int lvVictim, r;
					
					r = 3; // roznica levelu
			
					lvVictim = Victim.AsUnit.inherentlevel +r;
					// blokowanie expa
					if(.AsUnit.inherentlevel > lvVictim){
						int l, exp;
		  
						l = .AsUnit.inherentlevel; // level
						//maxexp = l-(l/2+l%(l/2)-1); // wzor na obliczanie maxexp z levelu
						exp = (l-(l/2+l%(l/2)-1))*(l-(l/2+l%(l/2)-1)+l%(l/2)-1)-1;  // wzor na obliczanie bazowaego experiance z levelu
						// oba wzory dzialaja od levelu 5+
								
						if(.AsUnit.experience > exp){
							.AsUnit.SetExperience(.AsUnit.experience - 1);
							if(.AsUnit.hero.IsValid){
								if(.AsUnit.hero.inherentlevel >= 10){
									int p, expp, s;
										
									l = .AsUnit.hero.inherentlevel;
									p = l+1;
										
									exp =  (l-(l/2+l%(l/2)-1))*(l-(l/2+l%(l/2)-1)+l%(l/2)-1)-1; 
									expp = (p-(p/2+p%(p/2)-1))*(p-(p/2+p%(p/2)-1)+p%(p/2)-1)-1;
									s = exp+((expp-1) - exp)/2;
									if(.AsUnit.hero.experience > s){
										.AsUnit.hero.SetExperience(.AsUnit.hero.experience - 1);
									}
								}
							}
						}
					}
				}
			}
			
			if(.AsHero.IsValid){
				if(.HasItem("Captain")){
					item = .FindItem("Captain");
				} else if(.HasItem("Officer")){
					item = .FindItem("Officer");
				} else if(.HasItem("General")){
					item = .FindItem("General");
				}
				if(item.IsValid){
					item.SetUseCount(item.use_count+1);
					ap = item.use_count;
				}
			} else {
				if(.HasItem("Trophy of war")){
					item = .FindItem("Trophy of war");
					if(item.IsValid){
						item.SetUseCount(item.use_count+1);
						ap = item.use_count;
					}
				} else {
					.AddItem("Trophy of war");
					ap = 1;
				}
			}
	
			if(!.AsHero.IsValid){
				int ki1, ki2, ki3;
			
				if(.IsHeirOf("LightInfantry")){
					ki1 = 2;
					ki2 = 4;
					ki3 = 8;
				} else if(.IsHeirOf("Piked")){
					ki1 = 4;
					ki2 = 8;
					ki3 = 16;
				} else if(.IsHeirOf("Horse") || .IsHeirOf("RangedHorse")){
					if(.IsHeirOf("EChariot") || .IsHeirOf("RChariot")){
						ki1 = 18;
						ki2 = 36;
						ki3 = 72;
					} else if(.IsHeirOf("CWarElephant")){
						ki1 = 30;
						ki2 = 60;
						ki3 = 120;
					} else {
						ki1 = 16;
						ki2 = 32;
						ki3 = 64;
					}
				} else if(.IsHeirOf("Melee")){
					if(.IsHeirOf("BVikingLord") || .IsHeirOf("GTridentWarrior")){
						ki1 = 14;
						ki2 = 28;
						ki3 = 56;
					} else {
						ki1 = 6;
						ki2 = 12;
						ki3 = 24;
					}
				} else if(.IsHeirOf("Ranged")){
					ki1 = 10;
					ki2 = 20;
					ki3 = 30;
				}
				
				if(ap >= ki1 && ap < ki2 && !.HasItem("Master")){
				
					CreateFeedback("DefensiveCry", .AsUnit, -1);
					.RemoveItemsOfType("Spoils of War");
					.AddItem("Master");
					if (rand(100) < 2)
						.AddItem("Promotion on Hero");
								
				} else if(ap >= ki2 && ap < ki3 && !.HasItem("Chieftain") && .HasItem("Master")){
								
					CreateFeedback("DefensiveCry", .AsUnit, -1);
					CreateFeedback("DefensiveCry", .AsUnit, -1); 
					CreateFeedback("DefensiveCry", .AsUnit, -1);
					.RemoveItemsOfType("Spoils of War"); 
					.RemoveItemsOfType("Master");
					.AddItem("Chieftain");
					  
					if(rand(100) < 3)
						.AddItem("Promotion on Hero");
							 
				} else if(ap >= ki3 && !.HasItem("Lord") && .HasItem("Chieftain")){
					
					CreateFeedback("DefensiveCry", .AsUnit, -1);
					CreateFeedback("DefensiveCry", .AsUnit, -1); 
					CreateFeedback("DefensiveCry", .AsUnit, -1); 
					CreateFeedback("DefensiveCry", .AsUnit, -1);
					CreateFeedback("DefensiveCry", .AsUnit, -1); 
					CreateFeedback("DefensiveCry", .AsUnit, -1);
					.RemoveItemsOfType("Spoils of War");
					.RemoveItemsOfType("Chieftain");
					.AddItem("Lord");
	  
					if(rand(100) < 5) 
						.AddItem("Promotion on Hero");
				} 
			} else {
				if(ap >= 5 && .HasItem("Officer") && !.HasItem("General") && !.HasItem("Leader")){
					item = .FindItem("Officer");
					.ExchangeItem(item, "General");
					item = .FindItem("General");
					item.SetUseCount(5);
				}
			}
			.RemoveItemsOfType("Damage charm");
			.RemoveItemsOfType("Armor charm");
			.RemoveItemsOfType("Health charm");
			.RemoveItemsOfType("Knowledge charm");
			
			if(EnvReadString(.player, "Path of the dead I") == "researched"){
				if (.AsUnit.inherentlevel < 4 )
				  .AsUnit.SetLevel(4);
			}

			if(EnvReadString(.player, "Path of the dead II") == "researched"){
				if (.AsUnit.inherentlevel < 8 )
				  .AsUnit.SetLevel(8);
			}

			if(EnvReadString(.player, "Path of the dead III") == "researched"){
				if (.AsUnit.inherentlevel < 12 )
				  .AsUnit.SetLevel(12);
			}

			if(EnvReadString(.player, "Spoils of war") == "researched"){
				if(!.HasItem("Spoils of War"))
					.AddItem("Spoils of War");
			}

			// rArena items
			if(EnvReadString(.player, "Veteran Offence") == "researched"){
				if (!.AsHero.IsValid && !.HasItem("Veteran Offence"))
				  .AddItem("Veteran Offence");
			}

			if(EnvReadString(.player, "Veteran Defence") == "researched"){
				if (!.AsHero.IsValid && !.HasItem("Veteran Defence"))
					.AddItem("Veteran Defence");
			}

			if(EnvReadString(.player, "Veteran Health") == "researched"){
				if (!.AsHero.IsValid && !.HasItem("Veteran Health"))
					.AddItem("Veteran Health");
			}

			// Britain
			if(.AsUnit.inherentlevel > 11){

				if (EnvReadString(.player, "Noble Offence") == "researched") {
					if (!.AsHero.IsValid && !.HasItem("Noble Offence"))
						.AddItem("Noble Offence");
				}

				if (EnvReadString(.player, "Noble Defence") == "researched") {
					if (!.AsHero.IsValid && !.HasItem("Noble Defence"))
						.AddItem("Noble Defence");
				}
			}

			if(EnvReadString(.player, "Gambler's Win") == "researched"){
				if (Victim){
					if (!Victim.IsSentry){
						if (.AsUnit.inherentlevel < Victim.AsUnit.inherentlevel){ 
							if (rand(100) < 20){
								.AsUnit.AddBonus(3,0,0,0,0);
							}
						}
					}
				}
			}

			if(EnvReadString(.player, "Gambler's Day") == "researched"){
				if(Victim){
					if(!Victim.IsSentry){
						if (.AsUnit.inherentlevel < Victim.AsUnit.inherentlevel){ 
							int chance;
							
							chance = EnvReadInt(.player, "GamblersDayChance");
							if(rand(100) < chance){
								int max_level, diff;
									
								max_level = 12;
								diff = Victim.AsUnit.inherentlevel - .AsUnit.inherentlevel;
								if(!.AsHero.IsValid){
									.AsUnit.SetLevel(Victim.AsUnit.inherentlevel);
								} else {
									if(max_level < diff) 
										.AsUnit.SetLevel(.AsUnit.inherentlevel+max_level);
									else
										.AsUnit.SetLevel(Victim.AsUnit.inherentlevel);
								}
							}
						}
					}
				}
			}
			if(Victim){
				if(Victim.IsHeirOf("BaseAnimal")){
					bool hunting = false;
					if (!.HasItem("Meat 1") && 
						!.HasItem("Meat 2") && 
						!.HasItem("Meat 3") && 
						!.HasItem("Meat 4")){
						hunting = true;
					} else {
						if(.race==Germany)
							hunting = true;
					}
					if(hunting){
						if(Victim.class == "Deer"){
								.AddItem("Meat 1"); 
						} else if(Victim.class == "Bear"
							 || Victim.class == "BearUnit"
							 || Victim.class == "Boar"
							 || Victim.class == "BoarUnit"){
								.AddItem("Meat 2");
						} else if(Victim.class == "LionM"
							 || Victim.class == "LionMUnit"
							 || Victim.class == "LionF"
							 || Victim.class == "LionFUnit"){
								.AddItem("Meat 3");
						} else if(Victim.class == "Wolf"
							 || Victim.class == "WolfUnit"){
								.AddItem("Meat 4");
						}
					}
				}
			}
		}
	}
}


